package com.mysteel.testssoclient.controller;

import cn.hutool.http.HttpUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * ProjectName: mail
 * ClassName: HelloController
 * Description:
 * date: 2021/03/13/013 11:35
 *
 * @author xiaochao
 */
@Controller
public class HelloController {

    @Value("${sso.server.url}")
    private String ssoServerUrl;

    /**
     * 无需登录即可访问
     * @return
     */
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    /**
     * 需要登录才可访问
     * @return
     */
    @GetMapping("/employee")
    public String employee(Model model ,String token,HttpSession session){
        if (StringUtils.isNotBlank(token)){
            // todo 根据token去认证服务器获取用户信息，将用户信息放到session中
            String username = HttpUtil.get("http://ssoserver.com:8080/userInfo?token=" + token);
            session.setAttribute("loginUser",username);
        }

        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null){
            return "redirect:" + ssoServerUrl + "?redirect_url=http://client1.com:8081/employee";
        }

        List<String> employee = new ArrayList<>(5);
        employee.add("张三");
        employee.add("李四");
        employee.add("王五");
        employee.add("赵六");
        employee.add("刘琦");
        model.addAttribute("employee",employee);
        return "list";
    }
}
